package com.zhuss.core.processor;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.wrongWord.CreateWrongWordBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.WrongWordDao;
import com.zhuss.core.db.entity.WrongWord;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;


/**
 * @description: 敏感字创建
 * @author: zhushanshuo
 * @create: Fri Nov 20 13:10:22 CST 2020
 **/
@Component("wrongWordCreate")
public class WrongWordCreate extends AbstractProcessor<Object, CreateWrongWordBo> implements Processor<Object, CreateWrongWordBo> {

    @Autowired
    private WrongWordDao wrongWordDao;
    
    @Autowired
	@Qualifier("wrongWordRead")
	private WrongWordRead wrongWordRead;
    
    
    /**
     * 校验
     */
    @Override
    public void check(CreateWrongWordBo bo) throws WpsException {
    	// TODO 创建 敏感字 校验， 请补充
    	String name = bo.getName();
    	if (name == null || "".equals(name.trim())) {
    		// throw new WpsException(ExceptionCode.敏感字name为空);
    	}
    	
    	List<WrongWord> list = wrongWordRead.list(name);
    	if (!list.isEmpty()) {
    		// throw new WpsException(ExceptionCode.敏感字name已存在);
    	}
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateWrongWordBo param) {
    	WrongWord record = new WrongWord();
    	record.setName(param.getName());
    	record.setStatus(param.getStatus());
    	record.setType(param.getType());
    	record.setSort(param.getSort());
    	record.setArea(param.getArea());

    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	wrongWordDao.create(record);
    	param.setTargetId(record.getWrongWordId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateWrongWordBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.敏感字创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
